﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public class ParameterSetting
    {
        public ParameterSetting(
            string axleName
            ,Axle.Direction dir
            ,double period
            ,double totalStep
            )
        {
            m_axleName = axleName;
            m_dir = dir;
            m_period = period;
            m_totalStep = totalStep;
            m_updateTime = ComputTimeStamp(); 
        }

        public Int64 ComputTimeStamp()
        {
            DateTime dt = DateTime.Now;
            return dt.Ticks * 1000 + dt.Millisecond;
        }

        private Int64 m_updateTime;

        public Int64 UpdateTime
        {
            get { return m_updateTime; }
            internal set { m_updateTime = value; }
        }

        private string m_axleName;

        public string AxleName
        {
            get { return m_axleName; }
            set { m_axleName = value; }
        }

        private Axle.Direction m_dir;

        public Axle.Direction Direction
        {
            get { return m_dir; }
            set { m_dir = value;
            //m_updateTime = ComputTimeStamp(); 
            }
        }

        private double m_period;

        public double Period
        {
            get { return m_period; }
            set { m_period = value;
            //m_updateTime = ComputTimeStamp(); 
            }
        }

        private double m_totalStep;

        public double TotalStep
        {
            get { return m_totalStep; }
            set { 
                m_totalStep = value;
                //m_updateTime = ComputTimeStamp(); 
            }
        }

        public bool Compare(string axleName
            , Axle.Direction dir
            , double period
            , double totalStep)
        {
            return m_axleName == axleName
                && m_dir.Value == dir.Value
                && m_period.ToString(MySetting.PositionDisDeciFormatter)
                    == period.ToString(MySetting.PositionDisDeciFormatter)
                && m_totalStep.ToString(MySetting.PositionDisDeciFormatter)
                    == totalStep.ToString(MySetting.PositionDisDeciFormatter);
        }

        public bool Compare(ParameterSetting ps)
        {
            return m_axleName == ps.m_axleName
                && m_dir.Value == ps.m_dir.Value
                && m_period.ToString(MySetting.PositionDisDeciFormatter)
                    == ps.m_period.ToString(MySetting.PositionDisDeciFormatter)
                && m_totalStep.ToString(MySetting.PositionDisDeciFormatter)
                    == ps.m_totalStep.ToString(MySetting.PositionDisDeciFormatter);
        }
    }

    public class ParameterCacheManager
    {
        private static Dictionary<string, ParameterSetting> s_pmList = new Dictionary<string, ParameterSetting>();
        private static object s_psLocker = new object();
        public static bool Compare(string axleName
            , Axle.Direction dir
            , double period
            , double totalStep)
        {
            ParameterSetting nps = new ParameterSetting(axleName,dir,period,totalStep);
            lock (s_psLocker)
            {
                if (s_pmList.ContainsKey(axleName))
                {
                    return s_pmList[axleName].Compare(nps);
                }
            }

            return false;
        }

        public static bool Update(string axleName
            , Axle.Direction dir
            , double period
            , double totalStep)
        {
            
            lock (s_psLocker)
            {
                ParameterSetting nps = new ParameterSetting(axleName, dir, period, totalStep);
                if (s_pmList.ContainsKey(axleName))
                {
                    if (nps.UpdateTime > s_pmList[axleName].UpdateTime)
                    {
                        s_pmList[axleName] = nps;
                    }
                }
                else
                {
                    s_pmList.Add(axleName, nps);
                }
            }
            return true;
        }
    }
}
